package com.example.malltest.admin.controller;

import com.example.malltest.admin.entity.Admin;
import com.example.malltest.admin.entity.AdminDTO;
import com.example.malltest.admin.service.AdminService;
import com.example.malltest.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 管理员控制层
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 验证密码登录
     * @param adminDTO
     * @return
     */
    @PostMapping("login")
    public R<Admin> login(@RequestBody @Validated AdminDTO adminDTO){
        Admin admin = adminService.login(adminDTO);
        return R.ok(admin);
    }

    /**
     * 获取信息
     * @param name
     * @return
     */
    @GetMapping("show")
    public R<Admin> show(@RequestParam String name){
        Admin admin = adminService.getAdmin(name);
        return R.ok(admin);
    }

    /**
     *
     * @param name
     * @param admin
     * @return
     */
    @PutMapping("edit")
    public R<Void> edit(@RequestParam String name,@RequestBody @Validated Admin admin){
        adminService.edit(name,admin);
        return R.ok();
    }

    /**
     *
     * @param name
     * @param OldPassword
     * @param NewPassword
     * @return
     */
    @PutMapping("updatePwd")
    public R<Void> updatePwd(@RequestParam String name,@RequestParam String OldPassword,@RequestParam String NewPassword){
        adminService.updatePwd(name,OldPassword,NewPassword);
        return R.ok();
    }

    /**
     * 上传头像
     * @param image
     * @return
     */
    @PostMapping("avatar")
    public R<String> uploadImage(@RequestParam String name, @RequestParam("image") MultipartFile image) {
        adminService.avatar(name,image);
        return R.ok("文件上传成功");
    }

    /**
     * 显示头像
     * @param name
     * @return
     */
    @GetMapping("showAvatar")
    public R<String> showAvatar(@RequestParam String name){
       String avatar = adminService.getAvatar(name);
       return R.ok(avatar);
    }
}
